package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 维修工单完成处理
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompleteHandler implements MsgHandler {
    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //解析数据
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg , TaskCompleteContract.class);

        //判断参数
        if (taskCompleteContract !=  null && StringUtils.isNotBlank(taskCompleteContract.getInnerCode())) {
            //查询设备
            VendingMachineEntity vm = vendingMachineService.getOne(Wrappers.<VendingMachineEntity>lambdaQuery()
                    .eq(VendingMachineEntity::getInnerCode, taskCompleteContract.getInnerCode()));
            //如果是投放工单
            if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
                //更新为运营状态
                vm.setVmStatus(VMSystem.VM_STATUS_RUNNING);
            //如果是撤机工单
            } else if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
                //更新为撤机状态
                vm.setVmStatus(VMSystem.VM_STATUS_REVOKE);
            }
            //更新数据
            vendingMachineService.updateById(vm);
        }
    }
}
